package com.lcm.openapi.generator;

import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Method;
import com.lcm.openapi.core.api.Api;
import com.lcm.openapi.core.model.ApiModel;
import com.lcm.openapi.core.api.ApiMethod;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DefaultNameGenerator implements NameGenerator {

    @Override
    public String generateModelName(ApiModel apiModel) {
        return apiModel.getModelName();
    }

    @Override
    public String generateApiName(Api api) {
        String baseUrl = api.getBaseUrl();
        if (baseUrl.equals("/") || StrUtil.isEmpty(baseUrl)) {
            return "Auto" + RandomUtil.randomNumbers(4) + "Controller";
        }
        //取最后一个/后面的字符串作为文件名称
        String apiName = baseUrl.substring(baseUrl.lastIndexOf("/") + 1);
        return StrUtil.upperFirst(apiName) + "Controller";
    }

    @Override
    public String generateApiMethodName(ApiMethod apiMethod) {
        String url = apiMethod.getUrl();
        String httpMethod = apiMethod.getHttpMethod();
        if (StrUtil.isNotEmpty(url)) {
            //替换掉{}
            url = url.replaceAll("\\{[^}]*\\}", "");
            List<String> urls = Stream.of(url.split("/")).filter(StrUtil::isNotEmpty).collect(Collectors.toList());
            if (urls.size() == 1) {
                return StrUtil.lowerFirst(httpMethod) + StrUtil.upperFirst(urls.get(0));
            } else {
                //url一般命令方式如/admin/role/detail admin一般为客户端前缀, 不作为命名依据，故skip(1)
//                String right = urls.stream().skip(1).map(StrUtil::upperFirst).collect(Collectors.joining());
                String right = urls.stream().map(StrUtil::upperFirst).collect(Collectors.joining());
                if(CharUtil.isNumber(right.charAt(0))){
                    right = right.substring(1);
                }
                right = modifyString(right);

                Method method = Method.valueOf(httpMethod.toUpperCase());
                String apiMethodName = null;
                switch (method) {
                    case GET:
                    case DELETE:
                        apiMethodName = httpMethod.toLowerCase() + right;
                        break;
                    case PUT:
                        apiMethodName = "update" + right;
                        break;
                    case POST:
                        if (urls.size() == 2) {
                            apiMethodName = "post" + right;
                        } else {
                            apiMethodName = StrUtil.lowerFirst(right);
                        }
                        break;
                }
                return apiMethodName;
            }
        }
        return StrUtil.lowerFirst(httpMethod) + RandomUtil.randomNumbers(4);
    }

    public static String modifyString(String input) {
        char[] chars = input.toCharArray();
        for (int i = 0; i < chars.length - 1; i++) {
            if (chars[i] == '-' || chars[i] == '.') {
                chars[i + 1] = Character.toUpperCase(chars[i + 1]);
            }
        }
        // 移除所有的 - 字符
        StringBuilder result = new StringBuilder();
        for (char c : chars) {
            if (c != '-' && c != '.') {
                result.append(c);
            }
        }
        return result.toString();
    }
}
